package main

import (
	"fmt"
	"os"
	"strings"

	"gitee.com/dingiyan/goMyAdmin/db"
	"gitee.com/dingiyan/goMyAdmin/web"
)

var appPort = "9900"

func main() {
	db.InitDB()
	fmt.Printf("app run on port %s\n", appPort)
	web.InitGinApp().Run(":" + appPort)
}

func init() {
	setEnv()
	setPort()
}
func setPort() {
	if len(os.Args) == 1 {
		return
	}
	for _, v := range os.Args {
		if strings.HasPrefix(v, "-p") || strings.HasPrefix(v, "--port") {
			portArgSlice := strings.Split(v, "=")
			if len(portArgSlice) == 2 && portArgSlice[1] != "" {
				appPort = portArgSlice[1]
				return
			}
			return
		}
	}

}

func setEnv() {
	fmt.Println(os.Args)
	exeFileName := os.Args[0]
	if strings.Contains(exeFileName, "__debug_") {
		os.Setenv("APP_RUN_MODE", "dev")
	} else {
		os.Setenv("APP_RUN_MODE", "prod")
	}
}
